package com.my.mymafengwo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.base.WithMediaPlayerActivity;
import com.my.mymafengwo.bean.SplashImage;
import com.my.mymafengwo.utils.ConstantUtils;
import com.my.mymafengwo.utils.UrlUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/2 16:10
 * copyright:
 * name:
 * step:
 * ***********************************************
 */
public class SplashActivity extends WithMediaPlayerActivity implements View.OnClickListener {

    private static final int DELAY_JUMP = 0;
    private static final int DELAY = 1500;
    private static final int TRAVELNOTES = 2;

    private SplashImage splashImage;

    /**
     * 欢迎页下方的视频控件
     */
    private SurfaceView sv;
    private ImageView iv;
    /**
     * 进入游记
     */
    private ImageView ivNote;
    private RelativeLayout rlIV;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DELAY_JUMP:
                    intoNextActivity();
            }
        }
    };

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initViews() {
        sv = (SurfaceView) findViewById(R.id.sv_splash);
        iv = (ImageView) findViewById(R.id.iv_splash);
        ivNote = (ImageView) findViewById(R.id.iv_splash_notes);
        rlIV = (RelativeLayout) findViewById(R.id.rl_splash);
    }

    @Override
    protected void registerListener() {
        ivNote.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        playMedia(sv, R.raw.splash_animation);
        ivNote.setEnabled(false);
        loadText();
    }

    /**
     * 下载上方显示的图片的字符串
     */
    private void loadText() {

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.SPLASH_IMAGE_URL, new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                String result = responseInfo.result.toString();
                if (result != null) {
                    splashImage = new Gson().fromJson(result, SplashImage.class);
                }
                //获取当前日前
                SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
                String date = sdf.format(new Date());
                String url = null;
                //获取与当前日期一致的图片
                for (SplashImage.DataEntity.ListEntity entity : splashImage.getData().getList()) {
                    if (splashImage.getData().getList() != null && splashImage.getData().getList().size() == 1) {
                        url = entity.getImages().get(0);
                        break;
                    }
                    if (entity.getName().startsWith(date)) {
                        url = entity.getImages().get(0);
                    }
                }
                if (url != null) {
                    loadImage(url);
                }
            }


            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    /**
     * 下载图片并加载
     *
     * @param url
     */
    private void loadImage(String url) {
        BitmapUtils bitmap = new BitmapUtils(this);
        bitmap.configDiskCacheEnabled(true);
        bitmap.configMemoryCacheEnabled(false);
        bitmap.configDefaultCacheExpiry(ConstantUtils.CACHEEXPIRY);
        bitmap.display(iv, url, new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                imageView.setImageBitmap(bitmap);
                ivNote.setEnabled(true);
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

            }
        });
    }

    /**
     * 下方视频播放完成，延时跳转下一页面
     *
     * @param mp
     */
    @Override
    public void onCompletion(MediaPlayer mp) {
        super.onCompletion(mp);
        handler.sendEmptyMessageDelayed(DELAY_JUMP, DELAY);
    }

    /**
     * 进入WelcomeActivity
     */
    private void intoNextActivity() {
        startActivity(new Intent(this, WelcomeActivity.class));
        finish();
    }

    /**
     * 点击事件的监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_splash_notes://点击进入游记
                Intent intent = new Intent(this, TravelNotesActivity.class);
                intent.putExtra("url", splashImage.getData().getList().get(0).getJump_url());
                startActivityForResult(intent, TRAVELNOTES);
                stopMedia();
                handler.removeMessages(DELAY_JUMP);
                break;
            default:
                break;
        }
    }

    /**
     * 从游记返回时，进入下一页面
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TRAVELNOTES:
                intoNextActivity();
                break;
            default:
                break;
        }
    }
}
